package top.mcwebsite.playandroid.business.search.impl.ui

import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import kotlin.math.roundToInt
import top.mcwebsite.playandroid.business.search.impl.vm.SearchUiState
import top.mcwebsite.playandroid.strings.Str

@Composable
fun SearchTopBar(
    uiState: SearchUiState,
    backClick: () -> Unit,
    onSearchKeywordChangeEvent: (String) -> Unit,
    onSearch: () -> Unit
) {
    Row(
        modifier = Modifier
            .height(56.dp)
            .fillMaxWidth(),
        verticalAlignment = Alignment.CenterVertically
    ) {
        IconButton(
            onClick = {
                backClick()
            }
        ) {
            Icon(imageVector = Icons.Default.ArrowBack, contentDescription = stringResource(id = Str.back))
        }

        BasicTextField(
            value = uiState.searchKeyword,
            onValueChange = {
                onSearchKeywordChangeEvent(it)
            },
            modifier = Modifier
                .defaultMinSize(minHeight = 35.dp)
                .weight(1f)
                .border(
                    width = 1.dp,
                    shape = RoundedCornerShape(25.dp),
                    color = MaterialTheme.colorScheme.onSurface,
                ),
            textStyle = MaterialTheme.typography.titleMedium.copy(color = LocalContentColor.current),
            cursorBrush = SolidColor(MaterialTheme.colorScheme.onSurface),
            decorationBox = @Composable { innerTextField ->
                Row(
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Icon(
                        imageVector = Icons.Default.Search, contentDescription = stringResource(id = Str.search_icon),
                        modifier = Modifier.padding(horizontal = 10.dp)
                    )
                    Box(modifier = Modifier.weight(1f)) {
                        if (uiState.searchKeyword.isEmpty()) {
                            Text(
                                text = stringResource(id = Str.search_tips),
                                style = LocalTextStyle.current.copy(
                                    color = LocalContentColor.current.copy(alpha = 0.5f)
                                )
                            )
                        }
                        innerTextField()
                    }
                }
            },
            maxLines = 1,
            keyboardOptions = KeyboardOptions(imeAction = ImeAction.Search),
            keyboardActions = KeyboardActions(onSearch = {
                onSearch()
            })
        )
        Text(stringResource(id = Str.search), modifier = Modifier
            .padding(5.dp)
            .clickable {
                onSearch()
            })
    }
}

